/**
* 
* Sonde system
* Copyright (c) 2009 Ecole Centrale de Nantes IRCCyN  
* Classe: 
* see <a href="{@docRoot}/Documentation/sonde.html"> for more information</a>
* 
* @author: Urunuela Richard
* @version 3.2
*/

package report;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Vector;

import storm.Entity;

public class classSonde {
	File temp;
	private Entity follow;
	Writer output = null;
	int nombreligne=0;
	/**
	 * The entity to sonde
	 * @param E
	 */
	public classSonde(Entity E ){
		this.follow= E;
		 try {
			 /**
			  * 
			  */
			 File dir = new File("/tmp");
			   if (!dir.exists()){
				   dir = new File("./tmp");
				   dir.mkdirs();
			   }
			 
 				temp = File.createTempFile(E.getName()+"-STORM-",".tmp",dir);
 			
				FileWriter t = new FileWriter(temp);
				output = new BufferedWriter(t);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 temp.deleteOnExit();
		
		
	}
	/**
	 *  print sonde information on output
	 */
	public void proceed() {
		try {
			
			output.write(follow.getKernel().getTime()+"##");
			String tmp = follow.SondetoString();
			if (tmp !=null)output.write(tmp);
			output.write("\n");
			nombreligne++;
			output.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 
	 * @return the number of line information
	 */
	public int getNombreligne() {
		return nombreligne;
	}
	public void setNombreligne(int nombreligne) {
		this.nombreligne = nombreligne;
	}
	/**
	 * 
	 * @return the line number n
	 */
	public Vector getligne(int n){
		Vector v = new Vector();
		try {
			FileReader t = new FileReader(temp);
			BufferedReader input = new BufferedReader(t);
			String line="";
			for (int i =0; i!= n;i++){
				line  = input.readLine().toString();
				
			}

			   String[] result = line.split("##");
			     for (int x=0; x<result.length; x++){
			    	 v.add(result[x]);
			     }
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return v;
		
	}
	

}
